<?php
namespace plugins\member\controller;
use cmf\controller\PluginAdminBaseController;
use think\Db;
use app\common\lib\model_operate\Transit;
use plugins\member\validate\MemberValidate;

class ShipAddressController extends PluginAdminBaseController{	
	/**	 * 收获地址列表	 */	
	public function index()	{		
		$data = $this->request->param();		
		$where = 1;		
		if (!empty($data['stype'])) {			
			$keyword = !empty($data['keyword']) ? $data['keyword'] : '';			
			switch ($data['stype']) {				
				case 1:					
					$where .= " and receive_name like '%".$keyword."%'";					
					break;				
				case 2:					
					$where .= " and receive_phone like '%".$keyword."%'";					
					break;				
				case 3:					
					$where .= " and uid like '%".$keyword."%'";					
					break;				
				case 4:					
					$where .= " and cri_code like '%".$keyword."%'";					
					break;				
				case 5:					
					$where .= " and address like '%".$keyword."%'";					
					break;			
			}		
		}		

		$list = Db::name('member_ship_address')->where($where)->order('uid','asc')->order('is_default','asc')->paginates(5);		
		$this->assign('list',$list);		
		$this->assign('data',$data);		
		$list->appends($data);		
		$this->assign('page',$list->render());
		//单独提取分页出来		
		return $this->fetch();	
	}		

	/**	 * add添加页面	 */	
	public function addPage()	
	{		
		$address = new \plugins\address\model\AddressModel();		
		$list = $address->getAddress();        
		$list = json_encode($list,JSON_UNESCAPED_UNICODE);		
		$this->assign('data',$list);		
		return $this->fetch();	
	}    

	//add添加操作	
	public function add(){		
		$param = $this->request->param();		
		if(!isset($param['uid']) || !isset($param['postal_code']) ||empty($param['receive_phone']) || empty($param['receive_name']) || empty($param['address']) || empty($param['key']) || empty($param['value'])){			
			return zy_json_echo(false,'传参错误',null,100);		
		}		
		$add['uid'] = $param['uid'];		
		//判断用户是否存在		
		$member = Db::name('member')->where('uid',$param['uid'])->find();        
		if( empty($member) ){            
			return zy_json_echo(false,'用户不存在',null,102);        
		}		
		$add['postal_code'] = $param['postal_code'];		
		$add['receive_phone'] = $param['receive_phone'];		
		$add['receive_name'] = $param['receive_name'];		
		$add['address'] = $param['address'];		
		$add['cri_code'] = $param['key'][2];  		
		$add['cri_name'] = $param['value'][2];  		
		$add['key'] = $param['key'];      		
		$add['value'] = $param['value'];      		
		$add['show_address']=json_encode(array_combine($add['key'],$add['value']),JSON_UNESCAPED_UNICODE);  		
		unset($add['key']);  		
		unset($add['value']);        
		// $add['show_address'] = implode(",",array_values($param['value']));        
		$add['is_default'] = 2;        
		$re = Db::name('member_ship_address')->insert($add);        
		if(empty($re)){			
			return zy_json_echo(false,'添加失败',$add,101);        
		}		
		return zy_json_echo(true,'添加成功',null,200);	
	}	

	/**	 * 修改页面	 */	
	public function editPage()	
	{	
		$param = $this->request->param();		
		$address = new \plugins\address\model\AddressModel();		
		$list = $address->getAddress();        
		$list = json_encode($list,JSON_UNESCAPED_UNICODE);        
		$da = Db::name('member_ship_address')->where('id',$param['id'])->find();        
		$da['show_address'] = !empty($da['show_address']) ? json_decode($da['show_address']) : [];        
		$mounted = [];        
		foreach($da['show_address'] as $key=>$value){        	
			$mounted[] = $key;        
		}        
		asort($mounted);        
		$mounted = array_values($mounted);        
		$mounted = json_encode($mounted,JSON_UNESCAPED_UNICODE);		
		$this->assign('data',$list);		
		$this->assign('da',$da);		
		$this->assign('mounted',$mounted);		
		return $this->fetch();	
	}	

	/**	 * 修改操作	 */	
	public function edit()	
	{		
		$param = $this->request->param();		
		if(!isset($param['uid']) || !isset($param['postal_code']) ||empty($param['receive_phone']) || empty($param['receive_name']) || empty($param['address']) || empty($param['id']) || empty($param['is_default'])) 
		{			
			return zy_json_echo(false,'传参错误',null,100);		
		}		
		if(!empty($param['key']) && !empty($param['value'])){			
			$update['cri_code'] = $param['key'][2];        	
			$update['cri_name'] = $param['value'][2];        	
			$key = $param['key'];    	  		
			$value = $param['value'];    	  		
			$update['show_address'] = json_encode(array_combine($key,$value),JSON_UNESCAPED_UNICODE);		
		}		
		$update['id'] = $param['id'];		
		$update['uid'] = $param['uid'];		
		$update['postal_code'] = $param['postal_code'];		
		$update['receive_phone'] = $param['receive_phone'];		
		$update['receive_name'] = $param['receive_name'];		
		$update['address'] = $param['address'];		
		$update['is_default'] = $param['is_default'];        
		if($param['is_default']==1){        	
			Db::name('member_ship_address')->where('uid',$param['uid'])->update(['is_default'=>2]);        
		}        
		$re = Db::name('member_ship_address')->update($update);        
		if(empty($re)){			
			return zy_json_echo(false,'修改失败',$update,101);        
		}		
		return zy_json_echo(true,'修改成功',null,200);	
	}	

	/**	 * 删除操作	 */	
	public function delAddress()	
	{		
		$param = $this->request->param();        
		if(empty($param['id'])){            
			return $this->error('未选择待删除数据');        
		}        
		if (is_array($param['id'])) {        	
			Db::name('member_ship_address')->where('id','in',$param['id'])->delete();        
		} else {        	
			Db::name('member_ship_address')->where('id',$param['id'])->delete();        
		}        
		return $this->success('删除成功');	
	}

}